package com.qd.p2p.web.action.user;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.UserCacheService;
import com.qdlc.p2p.biz.util.UserLoginUtil;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserIdentify;
import com.qdlc.p2p.dal.model.UserIdentifyModel;
import com.qdlc.p2p.dal.model.UserModel;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 用户认证信息
 *
 * @author sj
 * @version 2.0
 * @since 2014年2月20日
 */
public class UserIdentifyAction extends BaseAction<UserIdentifyModel> implements ModelDriven<UserIdentifyModel> {

    @Resource
    private UserCacheService userCacheService;

    private UserIdentifyModel userIdentifyModel = new UserIdentifyModel();

    @Override
    public UserIdentifyModel getModel() {
        return userIdentifyModel;
    }

    private User user;

    private Map<String, Object> data;

    /**
     * 显示更改头像页面
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/head")
    public String head() throws Exception {
        return "head";
    }

    /**
     * 认证页面
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/identify")
    public String identify() throws Exception {
        return "identify";
    }

    /**
     * 进行现场认证
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/doScene")
    public String doScene() throws Exception {
        data = new HashMap<String, Object>();
        data.put("result", true);
        data.put("msg", "现场认证申请提交成功，等待管理员审核！");
        printWebJson(getStringOfJpaObj(data));
        return null;
    }

    /**
     * 进行视频认证
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/doVideo")
    public String doVideo() throws Exception {
        data = new HashMap<String, Object>();
        data.put("result", true);
        data.put("msg", "视频认证申请提交成功，等待管理员审核！");
        printWebJson(getStringOfJpaObj(data));
        return null;
    }

    /**
     * 用户查询
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/realNameStatus")
    public String realNameStatus() throws Exception {
        user = UserLoginUtil.getLoginUser();
        data = new HashMap<String, Object>();
        UserIdentify userIdentify = UserUtils.getUserIdentify(user);
        if (userIdentify.getRealNameStatus() == 1) {
            data.put("result", true);
            data.put("msg", "已实名认证");
        } else {
            data.put("result", false);
            data.put("msg", "未实名认证");
        }

        printWebJson(getStringOfJpaObj(data));
        return null;
    }


    /**
     * vip申请页面
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/vip")
    public String vip() throws Exception {
        return "vip";
    }

    /**
     * 用户申请vip
     *
     * @return
     * @throws Exception
     */
    @Action("/member/useridentify/applyVip")
    public String applyVip() throws Exception {

        String vipRemark = paramString("vipRemark");
        if (StringUtil.isNull(vipRemark).length() > 125) {
            throw new BusinessException("备注字数限制在125内，请重新输入", 1);
        }

        User user = getSessionUser();
        UserModel userModel = UserModel.instance(user);
        userModel.setValidCode(paramString("validCode"));
        // 校验验证码
        userModel.validRegRule();
        // 后台传参
        Global.setTransfer("vipRemark", vipRemark);
        UserIdentify userIdentify = userCacheService.applyVip(user);

        // 更新缓存
        //TODO:UserIdentify
//        session.put(Constant.SESSION_USER_IDENTIFY, userIdentify);

        data = new HashMap<String, Object>();
        data.put("result", true);
        data.put("msg", "vip申请成功！");
        printWebJson(getStringOfJpaObj(data));

        return null;
    }

}
